/*
Web: http://www.jsForms.com
Copyright (C) 2008  Igor Demyanov (mailto:Igor.Demyanov@gmail.com)
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
jsForms.Components.HighlightElement={
	SetOffsetObject:function(obj)
	{
		this.P.OffsetObject = obj;
		this.P.Mode = 0;//0 - Edit, 1 - Info
		this.C.moveresize.draggable(
				{
					stop: this.OnDrag
				}
			).resizable({ 
			    handles: "e,s,se", 
			    knobHandles: true,
				stop:this.OnResize
			});
		return this;
	},
	OnDrag:function(e,ui)
	{
		var o = jsForms.GetObjectByType(this,'HighlightElement');
		if (!o)
		{
			return;
		}
		o._ExecEvent('onMove');
	},
	OnResize:function(e,ui)
	{
		var o = jsForms.GetObjectByType(this,'HighlightElement');
		if (!o)
		{
			return;
		}
		o._ExecEvent('onResize');
	},
	SetNode:function(node)
	{
		this.P.Node =node;
		if (node && node.Real) {
			$(node.Real).effect("highlight", {
				color: "red"
			}, 300);
		};
		return this.Update();
	},
	Update:function()
	{
		var node = this.P.Node;
		if (!node || !node.Real) {
			this.hide();
			return;
		}
		var eoff = $(node.Real).offset();
		var off;
		if (this.P.OffsetObject)
		{
			off = this.P.OffsetObject.offset();
		}else
		{
			off={letf:0,top:0};
		}
		var el = node.Real;
		var w = el.offsetWidth;//);
		var h = el.offsetHeight;//);
		var x =  eoff.left - off.left;
		var y = eoff.top - off.top;
		if (this.P.Mode == 0) {
			this.C.moveresize.css({
				left: x + 'px',
				top: y + 'px',
				width: w + 'px',
				height: h + 'px'
			}).show();
		}else
		{
			this.C.moveresize.hide();
		}
		this.C.info.css({top:y-20+"px",left:x+"px"});
		//iSize
		//iPosition
		this.C.iSize.text(w+'x'+h);
		return this.show();
	},
	ChangeMode: function(){
		if (this.P.Mode == 0)
		{
			this.P.Mode=1;
		}else
		{
			this.P.Mode=0;
		}
		this.Update();
	}
};

jsForms.CreateCompileInfoByObject(
{
	Name: 'HighlightElement',
	Properties: {
		//Text: {}
	},
	Events: {
		onResize: {},
		onMove: {}
	},
	Contents:{
	}
});
